//
//  WYXViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/16.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "WYXListCell.h"

#import "WYXViewController.h"

#import "CommonWYXTagModel.h"

@interface WYXViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray <CommonWYXTagModel *>*dataArray;

@property (nonatomic, strong) UITableView *tableview;

@end

@implementation WYXViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_WYX];
}

- (void)initView {
    
    self.title = @"无忧选服务";
    
    [self.view addSubview:self.tableview];
    
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.dataArray.count;
    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [WYXListCell calculateHeightWith:self.dataArray[indexPath.row]];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WYXListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYXListCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    CommonWYXTagModel *model = self.dataArray[indexPath.row];
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.labelUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.mainTitleLab.text = model.labelName;
    cell.subTitleLab.text = model.labelDesc;
    
    return cell;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"WYXListCell" bundle:nil] forCellReuseIdentifier:@"WYXListCellID"];
    }
    return _tableview;
}



- (void)request_WYX {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    [dic setValue:self.storeId forKey:@"sellerId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_WYXLab];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSArray *resultArray = [data valueForKey:@"data"];
                
                if ([resultArray isKindOfClass:[NSArray class]]) {
                    weakSelf.dataArray = @[].mutableCopy;
                    
                    for (int i = 0; i < resultArray.count; i++) {
                        CommonWYXTagModel *model = [CommonWYXTagModel modelWithDictionary:resultArray[i]];
                        [weakSelf.dataArray addObject:model];
                    }
                }

                [weakSelf.tableview reloadData];
                
            } else {
               [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
               [SVProgressHUD dismissWithDelay:1];
           }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}

@end
